﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace Sprout.Core.Security
{
    /// <summary>
    /// DES加解密
    /// </summary>
    public class DESHelper
    {
        /// <summary>
        /// DES 加密
        /// </summary>
        /// <param name="data">需要加密的数据</param>
        /// <param name="key">密钥（必须 8 位）</param>
        /// <param name="vector">向量（必须 8 位）</param>
        /// <returns></returns>
        public static string Encrypt(string data, string key, string vector = null)
        {
            DESCryptoServiceProvider desCryptoService = null;
            MemoryStream memoryStream = null;
            CryptoStream cryptoStream = null;
            try
            {
                string result = null;

                using (desCryptoService = new DESCryptoServiceProvider())
                {
                    byte[] byteDataList = Encoding.UTF8.GetBytes(data);
                    desCryptoService.Key = ASCIIEncoding.ASCII.GetBytes(key);

                    if (string.IsNullOrEmpty(vector)) vector = key;
                    desCryptoService.IV = ASCIIEncoding.ASCII.GetBytes(vector);

                    using (memoryStream = new MemoryStream())
                    {
                        using (cryptoStream = new CryptoStream(memoryStream, desCryptoService.CreateEncryptor(), CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(byteDataList, 0, byteDataList.Length);
                            cryptoStream.FlushFinalBlock();
                        }
                        result = Convert.ToBase64String(memoryStream.ToArray());
                    }
                }
                return result;
            }
            catch
            {
                throw;
            }
            finally
            {
                if (cryptoStream != null) cryptoStream.Close();
                if (memoryStream != null) memoryStream.Close();
                if (desCryptoService != null) desCryptoService.Clear();
            }
        }
        /// <summary>
        /// DES 解密
        /// </summary>
        /// <param name="data">需要解密的数据</param>
        /// <param name="key">密钥（必须 8 位）</param>
        /// <param name="vector">向量（必须 8 位）</param>
        /// <returns></returns>
        public static string Decrypt(string data, string key, string vector = null)
        {
            DESCryptoServiceProvider desCryptoService = null;
            MemoryStream memoryStream = null;
            CryptoStream cryptoStream = null;
            try
            {
                string result = "";
                byte[] byteDataList = Convert.FromBase64String(data);
                using (desCryptoService = new DESCryptoServiceProvider())
                {
                    desCryptoService.Key = ASCIIEncoding.ASCII.GetBytes(key);

                    if (string.IsNullOrEmpty(vector)) vector = key;
                    desCryptoService.IV = ASCIIEncoding.ASCII.GetBytes(vector);

                    using (memoryStream = new MemoryStream())
                    {
                        using (cryptoStream = new CryptoStream(memoryStream, desCryptoService.CreateDecryptor(), CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(byteDataList, 0, byteDataList.Length);
                            cryptoStream.FlushFinalBlock();
                        }
                        result = Encoding.UTF8.GetString(memoryStream.ToArray());
                    }
                }
                return result;
            }
            catch
            {
                throw;
            }
            finally
            {
                if (cryptoStream != null) cryptoStream.Close();
                if (memoryStream != null) memoryStream.Close();
                if (desCryptoService != null) desCryptoService.Clear();
            }
        }
    }
}
